﻿<?php

//////////////////////////////////////////////////////////////////////////
//
// WikiHiero2 - A PHP convert from text using "Manual for the encoding of
// hieroglyphic texts for computer input" syntax to HTML entities (table and
// images).
//
// Copyright (C) 2004-2007 Guillaume Blanchard (Aoineko)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//////////////////////////////////////////////////////////////////////////

//--------------------------------------------------
// G L O B A L   D E F I N E S
//--------------------------------------------------
define("WH_URL",    1);
define("WH_POST",   2);
define("WH_COOKIE", 3);
define("WH_FILE",	4);

define("WH_DEBUG",	true);
define("WH_ASSERT",	false);

define("WH_VER_MAJ", 2);
define("WH_VER_MED", 0);
define("WH_VER_MIN", "0 (beta)");

$wh_Null = null;

//--------------------------------------------------
// V E R S I O N
//--------------------------------------------------

/** Get WikiHiero version string */
function WH_GetVersion()
{
	return WH_VER_MAJ.".".WH_VER_MED.".".WH_VER_MIN;
}

/** Get WikiHiero core system credit */
function WH_GetCredit()
{
	return "WikiHiero ".WH_VER_MAJ.".".WH_VER_MED.".".WH_VER_MIN." by Guillaume Blanchard (Aoineko) under GPL (2004-2010).";
}

//--------------------------------------------------
//
//--------------------------------------------------
/**
 * Call a static method from strings (to handle pre 5.3.1 PHP server)
 * @param $className The class name
 * @param $functionName The static method name
 */
function WH_CallStatic($className, $functionName)
{
	/*if(version_compare(phpversion(), '5.3.1') >= 0)
	{
		return $className::$functionName();
	}
	else*/
	{
		$class = new $className;
		return $class->$functionName();
	}
}

//--------------------------------------------------
// E R R O R   H A N D L E I N G
//--------------------------------------------------
$wh_LastError = 0;
$wh_Warnings = "";

define("WH_ERROR_INVALID_PARAM", 0);

/**
 * Set last error code.
 * @param $error	The error to stack.
 */
function WH_SetLastError($error)
{
	global $wh_LastError;
	$wh_LastError = $error;
	if(WH_ASSERT)
		die("Error $wh_LastError\n");
}

/** Get last error */
function WH_GetLastError()
{
	global $wh_LastError;
	return $wh_LastError;
}

/**
 * Debug message
 * @param $msg	Debug message
 */
function WH_Debug($msg)
{
	global $wh_Warnings;
	if(WH_DEBUG)
		$wh_Warnings .= "$msg\n";
		//echo "$msg\n";
}

/**
 * Assert message
 * @param $msg	Debug message
 */
function WH_Assert($msg)
{
	if(WH_ASSERT)
		die("Error: $msg\n");
	else
		WH_Debug($msg);
}

//--------------------------------------------------
// F O R M   V A R I A B L E S   H A N D L E I N G
//--------------------------------------------------

/**
 * Get PHP variable
 * @param $source	Variable source (URL, POST, etc.)
 * @param $param	Variable name
 * @param $default	Default variable value
 */
function WH_GetVar($source, $param, $default)
{
	if($source == WH_URL)
		return WH_GetParam($param, $default);
	else if($source == WH_POST)
		return WH_GetPost($param, $default);
	else if($source == WH_COOKIE)
		return WH_GetCookie($param, $default);
	else if($source == WH_FILE)
		return WH_GetFile($param, $default);
		else
		die("Unknow variable source!");
}

/**
 * Get variable from URL
 * @param $param	Variable name
 * @param $default	Default variable value
 */
function WH_GetParam($param, $default)
{
	if(isset($_GET[$param]))
		return $_GET[$param];
	else
		return $default;
}

/**
 * Get variable from POST
 * @param $param	Variable name
 * @param $default	Default variable value
 */
function WH_GetPost($param, $default)
{
	if(isset($_POST[$param]))
		return $_POST[$param];
	else
		return $default;
}

/**
 * Get variable from Cookie
 * @param $param	Variable name
 * @param $default	Default variable value
 */
function WH_GetCookie($param, $default)
{
	if(isset($_COOKIE[$param]))
		return $_COOKIE[$param];
	else
		return $default;
}

/**
 * Get variable from file
 * @param $param	Variable name
 * @param $default	Default variable value
 */
function WH_GetFile($param, $default)
{
	if(isset($_FILE[$param]))
		return $_FILE[$param]["name"];
	else
		return $default;
}


?>